package feri.lenobe;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class PrevodOCR extends Activity  {
	  private static final String TAG = "CameraDemo";
	  PredogledKamere predogledKamere; // <1>
	  Button buttonClick; // <2>

	  /** Called when the activity is first created. */
	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.prevodocr);
	    try
	    {
		    predogledKamere = new PredogledKamere(this.getApplicationContext()); // <3>
		    ((FrameLayout) findViewById(R.id.predogled)).addView(predogledKamere); // <4>
	
		    buttonClick = (Button) findViewById(R.id.buttonClick);
		    buttonClick.setOnClickListener(new OnClickListener() {
		      public void onClick(View v) { // <5>
		        predogledKamere.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
		      }
		    });
	
		    Log.d(TAG, "onCreate'd");
	    }
	    catch(Exception e)
	    {
	    	Toast.makeText(this, e.toString(), 1200);
	    }
	  }

	  // Called when shutter is opened
	  ShutterCallback shutterCallback = new ShutterCallback() { // <6>
	    public void onShutter() {
	      Log.d(TAG, "onShutter'd");
	    }
	  };

	  // Handles data for raw picture
	  PictureCallback rawCallback = new PictureCallback() { // <7>
	    public void onPictureTaken(byte[] data, Camera camera) {
	      Log.d(TAG, "onPictureTaken - raw");
	    }
	  };

	  // Handles data for jpeg picture
	  PictureCallback jpegCallback = new PictureCallback() { // <8>
	    public void onPictureTaken(byte[] data, Camera camera) {
	      FileOutputStream outStream = null;
	      try {
	        // Write to SD Card
	        outStream = new FileOutputStream(String.format(Environment.getExternalStorageDirectory()+"/%d.jpg",
	            System.currentTimeMillis())); // <9>
	        outStream.write(data);
	        outStream.close();
	        Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
	      } catch (FileNotFoundException e) { // <10>
	        e.printStackTrace();
	      } catch (IOException e) {
	        e.printStackTrace();
	      } finally {
	      }
	      Log.d(TAG, "onPictureTaken - jpeg");
	    }
	  };
}
